Skip to main content
Version: Current

Create Your First Chart

This tutorial will guide you through creating your first visualization with Muze, from data loading to final rendering.

Setting Up Your Data

Supported Data Formats

Muze works with DataModel as its primary data source. You can load data in any of these formats:

  • Delimiter separated values (CSV, TSV)
  • Flat JSON structure
  • Matrix of values (2D array)

Sample Dataset

Here's a sample from the Cars dataset we'll use in this tutorial:

NameMiles_per_GallonCylindersDisplacementHorsepowerWeight_in_lbsAccelerationYearOrigin
chevrolet chevelle malibu1883071303504121/1/1970USA
buick skylark 320158350165369311.51/1/1970USA
plymouth satellite1883181503436111/1/1970USA
amc rebel sst1683041503433121/1/1970USA

Creating a DataModel

1. Define Your Schema

The schema defines the characteristics of your data fields:

const schema = [
{
name: "Maker",
type: "dimension",
},
{
name: "Horsepower",
type: "measure",
},
];

2. Initialize DataModel

// Load the DataModel class
const DataModel = muze.DataModel;

// Parse the data
const parsedData = await DataModel.loadData(data, schema);

// Create DataModel instance
const dm = new DataModel(parsedData);

Building the Visualization

1. Create a Canvas

The canvas serves as a container for your visualization:

const { muze } = viz;
const canvas = muze.canvas();

// Attach DataModel to canvas
canvas.data(dm);

2. Configure the Layout

Define which fields appear on which axes:

canvas
.rows(["Horsepower"]) // Y-axis
.columns(["Maker"]); // X-axis

3. Set Dimensions

You can set the visualization size in two ways:

CSS Approach
#chart {
width: 500px;
height: 300px;
}
API Approach
canvas.width(500).height(300);

4. Render the Chart

Mount the visualization to a DOM element:

canvas.mount("#chart");

Complete Example

Here's a full implementation that puts everything together:

const { muze, getDataFromSearchQuery, env } = viz;
const DataModel = muze.DataModel;

// Sample data structure
const data = {
// ... data shown in table above ...
};

// Define schema
const schema = [
{
name: "Maker",
type: "dimension",
},
{
name: "Horsepower",
type: "measure",
},
// ... additional fields
];

// Create and configure visualization
const formattedData = await DataModel.loadData(data, schema);
const dm = new DataModel(formattedData);

muze
.canvas()
.data(dm)
.rows(["Horsepower"])
.columns(["Maker"])
.mount("#chart");
Best Practices
  • Always define a complete schema for your data
  • Use appropriate field types (dimension/measure)
  • Consider container size when setting dimensions
  • Mount to a specific DOM element ID